<?php
App::uses('AppController', 'Controller');
/**
 * Documents Controller
 *
 * @property Document $Document
 */
class DocumentsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Document->recursive = 0;
		$this->set('documents', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Document->exists($id)) {
			throw new NotFoundException(__('Invalid document'));
		}
		$options = array('conditions' => array('Document.' . $this->Document->primaryKey => $id));
		$this->set('document', $this->Document->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Document->create();
			if ($this->Document->save($this->request->data)) {
				$this->Session->setFlash(__('The document has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The document could not be saved. Please, try again.'));
			}
		}
		$users = $this->Document->User->find('list');
		$this->set(compact('users'));
	}
	
	public function upload() {
		if ($this->request->is('post')) {
			$this->Document->create();
			if ($this->uploadDoc() && $this->Document->save($this->data)) {
				$this->Session->setFlash(__('The document was uploaded successfully'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The document could not be saved. Please try again.'));
			}
		}
		$users = $this->Document->User->find('list');
		$this->set(compact('users'));
	}
	
	function uploadDoc() {
		$file = $this->data['Document']['file'];
		if ($file['error'] === UPLOAD_ERR_OK) {
			$id = String::uuid();
			if (move_uploaded_file($file['tmp_name'], APP.'documents\\'.$id)) {
				$this->request->data['Document']['id'] = $id;
				$this->request->data['Document']['user_id'] = $this->Auth->user('id');
				$this->request->data['Document']['uploader'] = $this->Auth->user('id');
				$this->request->data['Document']['filename'] = $file['name'];
				$this->request->data['Document']['filesize'] = $file['size'];
				$this->request->data['Document']['filemime'] = $file['type'];
				return true;
			}
		}
		return false;
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Document->exists($id)) {
			throw new NotFoundException(__('Invalid document'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Document->save($this->request->data)) {
				$this->Session->setFlash(__('The document has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The document could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Document.' . $this->Document->primaryKey => $id));
			$this->request->data = $this->Document->find('first', $options);
		}
		$users = $this->Document->User->find('list');
		$this->set(compact('users'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Document->id = $id;
		if (!$this->Document->exists()) {
			throw new NotFoundException(__('Invalid document'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Document->delete()) {
			$this->Session->setFlash(__('The document was deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The document could not be deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	function download($id) {
    $document = $this->Document->findById($id);
    $this->viewClass = 'Media';
    $filename = $document['Document']['filename'];
    $this->set(array(
        'id' => $document['Document']['id'],
        'name' => substr($filename, 0, strrpos($filename, '.')),
        'mimeType'  => $document['Document']['mimetype'],
        'path' => 'documents'.DS,
        'download' => true,
    ));
}
		
	
	
}

	